package approve.action;

import java.util.Timer;
import java.util.TimerTask;

import model.pojo.Employee;
import nguyen.dao.ReceiverDocumentDAO;

import project.active.LogoutActivity;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

public class CustomActivity extends Activity {
	
	protected void onCreate(android.os.Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		
		Employee em=(Employee) getIntent().getExtras().get("em");
		idEmployee=em.getIdemployee();
		dataBase=getIntent().getExtras().getString("database");
		
		showNotificationMessage();
	};
	
	Timer timer=new Timer();
	NotificationManager mNotificationManager=null;
	private int SIMPLE_NOTFICATION_ID;
	String idEmployee, dataBase;
	
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu_home, menu);
		return true;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		if (item.getItemId() == R.id.homeItem) {
			killNotification();
			
			Intent i = new Intent(this, MainFrame.class);		
			i.putExtras(getIntent().getExtras());
			startActivity(i);

		} else if (item.getItemId() == R.id.logoutItem) {
			killNotification();
			
			Intent i=new Intent(this, LogoutActivity.class);
			i.putExtra("database", dataBase);
			startActivity(i);
		}

		return super.onMenuItemSelected(featureId, item);
	}
	
	private void showNotificationMessage(){
		timer.scheduleAtFixedRate(new mainTask(), 500, 50000);
	//	
	}
	
	public void killNotification(){
		timer.cancel();
		mNotificationManager.cancel(SIMPLE_NOTFICATION_ID);
	}
	
	ReceiverDocumentDAO rd = new ReceiverDocumentDAO();
	private class mainTask extends TimerTask {

		@Override
		public void run() {
			mNotificationManager.cancel(SIMPLE_NOTFICATION_ID);
			final Notification notifyDetails;
			int count=rd.countAllNewMessage(idEmployee, dataBase);
			if( count==0 || count==1){
			
			notifyDetails = new Notification(R.drawable.ic_launcher,
					count + " new message", 500);
			}
			else{
				notifyDetails = new Notification(R.drawable.ic_launcher,
						count + " new messages", 500);
			}
//			long[] vibrate = { 500, 600, 200, 300 };
//			notifyDetails.vibrate = vibrate;
			notifyDetails.defaults = Notification.DEFAULT_ALL;
			Context context = getApplicationContext();
			CharSequence contentTitle = "Schogini Systems. Simple Notification";
			CharSequence contentText = "Get back to Application on clicking me";

			Intent notifyIntent = new Intent(context, MainFrame.class);

			PendingIntent intent = PendingIntent.getActivity(CustomActivity.this,
					0, notifyIntent,
					android.content.Intent.FLAG_ACTIVITY_NEW_TASK);

			notifyDetails.setLatestEventInfo(context, contentTitle,
					contentText, intent);
	//
			mNotificationManager.notify(SIMPLE_NOTFICATION_ID,
					notifyDetails);
//			mNotificationManager.cancel(SIMPLE_NOTFICATION_ID);
//			
		}
		
	}
}
